package com.cskaoyan.service.admin;

import com.cskaoyan.bean.MarketSystem;
import com.cskaoyan.bean.MarketSystemExample;
import com.cskaoyan.mapper.MarketSystemMapper;
import com.cskaoyan.model.admin.vo.MallConfigRespVO;
import com.cskaoyan.model.admin.vo.SelectWxVO;
import org.apache.commons.beanutils.BeanUtilsBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.InvocationTargetException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName: ConfigServiceImpl
 * @Author: WangEnzhao
 * @Description:
 * @CreateTime: 2021/11/20:20:00
 */
@Service
public class ConfigServiceImpl implements ConfigService {

    @Autowired
    MarketSystemMapper systemMapper;

    /**
     * @Author: WangEnzhao
     * @Description:
     * @CreateTime: 查询商场配置
     * @Param:
     * @return:
     **/
    @Override
    public MallConfigRespVO selectMallConfig() {
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        List<MarketSystem> marketSystems = systemMapper.selectByExample(marketSystemExample);
        MallConfigRespVO mallConfigRespVO = new MallConfigRespVO();
        //将marketSystems中的keyName和keyValue转化为map
        Map<String, String> map = new HashMap<>();
        for (MarketSystem marketSystem : marketSystems) {
            map.put(marketSystem.getKeyName(),marketSystem.getKeyValue());
        }
        //将map变成MallConfigRespVO
        try {
            BeanUtilsBean.getInstance().populate(mallConfigRespVO,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return mallConfigRespVO;
    }


    /**
     * @Author: WangEnzhao
     * @Description:
     * @CreateTime: 修改商场配置
     * @Param:
     * @return:
     **/
    @Transactional
    @Override
    public int updateMallConfig(Map<String, String> mallConfigMap) {
        for (Map.Entry<String, String> entry : mallConfigMap.entrySet()) {
            MarketSystemExample marketSystemExample = new MarketSystemExample();
            marketSystemExample.createCriteria().
                    andKeyNameEqualTo(entry.getKey());
            MarketSystem marketSystem = new MarketSystem();
            marketSystem.setKeyValue(entry.getValue());
            int affectedRows = systemMapper.updateByExampleSelective(marketSystem, marketSystemExample);
        }
        return 0;
    }

    /**
     * @Author: WangEnzhao
     * @Description: express配置查询
     * @CreateTime:
     * @Param:
     * @return:
     **/
    @Override
    public Map selectExpressConfig() {
        //返回的数据
        HashMap<String, String> result = new HashMap<>();

        //查询marketExpressFreightMin
        MarketSystemExample marketSystemExample1 = new MarketSystemExample();
        marketSystemExample1.createCriteria().andKeyNameEqualTo("market_express_freight_min");
        List<MarketSystem> marketSystems = systemMapper.selectByExample(marketSystemExample1);
        String marketExpressFreightMin = marketSystems.get(0).getKeyValue();
        result.put("market_express_freight_min", marketExpressFreightMin);

        //查询market_express_freight_value
        MarketSystemExample marketSystemExample2 = new MarketSystemExample();
        marketSystemExample2.createCriteria().andKeyNameEqualTo("market_express_freight_value");
        List<MarketSystem> marketSystems1 = systemMapper.selectByExample(marketSystemExample2);
        String marketExpressFreightValue = marketSystems1.get(0).getKeyValue();
        result.put("market_express_freight_value", marketExpressFreightValue);
        return result;
    }

    /**
     * @Author: WangEnzhao
     * @Description: express update配置
     * @CreateTime:
     * @Param:
     * @return:
     **/
    @Transactional
    @Override
    public int updateExpressConfig(Map<String, String> updateExpressMap) {
        for (Map.Entry<String, String> entry : updateExpressMap.entrySet()) {
            updateSystemByKeyName(entry.getKey(),entry.getValue());
        }
        return 0;
    }

    /**
    * @Author: WangEnzhao
    * @Description: 获取order的配置
    * @CreateTime:
    * @Param:
    * @return:
    **/
    @Override
    public Map selectOrderConfig() {
        //返回的数据
        HashMap<String, String> result = new HashMap<>();
        MarketSystem comment = selectMarketSystemByKeyName("market_order_comment");
        MarketSystem unConfirm = selectMarketSystemByKeyName("market_order_unconfirm");
        MarketSystem unPaid = selectMarketSystemByKeyName("market_order_unpaid");
        result.put("market_order_comment",comment.getKeyValue());
        result.put("market_order_unconfirm",unConfirm.getKeyValue());
        result.put("market_order_unpaid",unPaid.getKeyValue());
        return result;
    }

    /**
    * @Author: WangEnzhao
    * @Description: 更新order配置
    * @CreateTime:
    * @Param:
    * @return:
    **/
    @Override
    public void updateOrderConfig(Map<String, String> updateOrderMap) {
        for (Map.Entry<String, String> entry : updateOrderMap.entrySet()) {
            updateSystemByKeyName(entry.getKey(),entry.getValue());
        }
    }

    /**
    * @Author: WangEnzhao
    * @Description: 获取微信小程序配置
    * @CreateTime:
    * @Param:
    * @return:
    **/
    @Override
    public SelectWxVO selectWxConfig() {
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        List<MarketSystem> marketSystems = systemMapper.selectByExample(marketSystemExample);
        //将marketSystem中key和value存储到map中
        HashMap<String, String> map = new HashMap<>();
        for (MarketSystem marketSystem : marketSystems) {
            map.put(marketSystem.getKeyName(),marketSystem.getKeyValue());
        }
        //用beanUtils赋值
        SelectWxVO selectWxVO = new SelectWxVO();
        try {
            BeanUtilsBean.getInstance().populate(selectWxVO,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        return selectWxVO;
    }

    /**
    * @Author: WangEnzhao
    * @Description: 更新微信小程序配置
    * @CreateTime:
    * @Param:
    * @return:
    **/
    @Override
    public void updateWxConfig(Map<String, String> updateWxConfigMap) {
        for (Map.Entry<String, String> entry : updateWxConfigMap.entrySet()) {
            updateSystemByKeyName(entry.getKey(),entry.getValue());
        }
    }


    /**
     * @Author: WangEnzhao
     * @Description: 通过keyName和keyValue更新market_system表
     * @CreateTime:
     * @Param:
     * @return:
     **/
    private void updateSystemByKeyName(String keyName, String keyValue) {
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        marketSystemExample.createCriteria().andKeyNameEqualTo(keyName);
        MarketSystem marketSystem = new MarketSystem();
        marketSystem.setKeyValue(keyValue);
        systemMapper.updateByExampleSelective(marketSystem, marketSystemExample);
    }

    /**
    * @Author: WangEnzhao
    * @Description: 通过keyName获取market_system信息
    * @CreateTime:
    * @Param:
    * @return:
    **/
    private MarketSystem selectMarketSystemByKeyName(String keyName) {
        MarketSystemExample marketSystemExample = new MarketSystemExample();
        marketSystemExample.createCriteria().andKeyNameEqualTo(keyName);
        List<MarketSystem> marketSystems = systemMapper.selectByExample(marketSystemExample);
        return marketSystems.get(0);
    }


}
